/**
 * 
 */
package com.iss.card.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.iss.card.model.MarkedProductDetailInfo;
import com.iss.card.model.ProductDetailInfo;

/**
 * @author dtdh
 * 
 */
public final class MarkProductManager {
	private final static String TAG = MarkProductManager.class.getSimpleName();
	private static MarkProductManager sInstance;
	private final static String SP_KEY_MARKED_PRODUCT = "SP_KEY_MARKED_PRODUCT";
	private final static String SP_PRODUCT_SEPERATOR = "@#@";
	private final static String SP_PRODUCT_DETAIL_SEPERATOR = "@###@";

	private Map<String, MarkedProductDetailInfo> mProdutMaps = new HashMap<String, MarkedProductDetailInfo>();

	public synchronized static MarkProductManager instance() {
		if (sInstance == null) {
			sInstance = new MarkProductManager();
		}
		return sInstance;
	}

	private MarkProductManager() {
		mProdutMaps = readMarkedProductList();
	}

	public boolean isMarkedProduct(ProductDetailInfo info) {
		String key = info.getStoreID() + SP_PRODUCT_DETAIL_SEPERATOR
				+ info.getProductID();
		return mProdutMaps.containsKey(key);
	}

	/**
	 * save marked product detail info into disk.
	 * 
	 * @param info
	 *            product information.
	 * @param isMark
	 *            true means marked product otherwise unmark.
	 */
	public void saveMarkedProductInfo(ProductDetailInfo info, boolean isMark) {
		String key = info.getStoreID() + SP_PRODUCT_DETAIL_SEPERATOR
				+ info.getProductID();
		Log.d(TAG,
				"saveMarkedProductInfo: sid=" + info.getStoreID() + ", pid="
						+ info.getProductID() + ", url="
						+ info.getProductPicUrl() + ", W="
						+ info.getProductPicW() + ", H="
						+ info.getProductPicH() + ", del="
						+ info.getProductDeleted());
		if (isMark) {
			MarkedProductDetailInfo product = new MarkedProductDetailInfo();
			product.setProductDeleted(info.getProductDeleted());
			product.setProductID(info.getProductID());
			product.setProductMarked(true);
			product.setProductPicH(info.getProductPicH());
			product.setProductPicUrl(info.getProductPicUrl());
			product.setProductPicW(info.getProductPicW());
			product.setStoreID(info.getStoreID());
			mProdutMaps.put(key, product);
		} else {
			mProdutMaps.remove(key);
		}
		writeMarkedProductList(mProdutMaps);
	}

	/**
	 * get marked product list.
	 * 
	 * @return product list.
	 */
	public List<MarkedProductDetailInfo> getMarkedProductList() {
		List<MarkedProductDetailInfo> list = new ArrayList<MarkedProductDetailInfo>();
		Iterator<String> itKey = mProdutMaps.keySet().iterator();
		while (itKey.hasNext()) {
			list.add(mProdutMaps.get(itKey.next()));
		}
		return list;
	}

	/**
	 * write marked product maps into shared preference.
	 * 
	 * @param productMaps
	 *            marked product maps.
	 */
	private void writeMarkedProductList(
			Map<String, MarkedProductDetailInfo> productMaps) {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		Editor ed = sp.edit();
		StringBuffer products = new StringBuffer();
		Iterator<String> itKey = productMaps.keySet().iterator();
		while (itKey.hasNext()) {
			MarkedProductDetailInfo info = productMaps.get(itKey.next());
			products.append(info.getStoreID() + SP_PRODUCT_DETAIL_SEPERATOR
					+ info.getProductID() + SP_PRODUCT_DETAIL_SEPERATOR
					+ info.getProductPicUrl() + SP_PRODUCT_DETAIL_SEPERATOR
					+ info.getProductPicW() + SP_PRODUCT_DETAIL_SEPERATOR
					+ info.getProductPicH() + SP_PRODUCT_DETAIL_SEPERATOR
					+ info.isProductMarked() + SP_PRODUCT_DETAIL_SEPERATOR
					+ info.getProductDeleted());
			products.append(itKey.hasNext() ? SP_PRODUCT_SEPERATOR : "");
		}
		ed.putString(SP_KEY_MARKED_PRODUCT, products.toString());
		ed.commit();
	}

	/**
	 * read marked raw data from shared preferences.
	 * 
	 * @return marked product maps.
	 */
	private Map<String, MarkedProductDetailInfo> readMarkedProductList() {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		String[] products = sp.getString(SP_KEY_MARKED_PRODUCT, "").split(
				SP_PRODUCT_SEPERATOR);
		Map<String, MarkedProductDetailInfo> productMaps = new HashMap<String, MarkedProductDetailInfo>();
		// there is no product.
		if (products.length == 0) {
			return productMaps;
		}

		for (String product : products) {
			String[] productDetailInfos = product
					.split(SP_PRODUCT_DETAIL_SEPERATOR);
			if (productDetailInfos.length <= 1) {
				continue;
			}
			MarkedProductDetailInfo info = new MarkedProductDetailInfo();
			info.setStoreID(productDetailInfos[0]);
			info.setProductID(Integer.valueOf(productDetailInfos[1]));
			info.setProductPicUrl(productDetailInfos[2]);
			info.setProductPicW(Integer.valueOf(productDetailInfos[3]));
			info.setProductPicH(Integer.valueOf(productDetailInfos[4]));
			info.setProductMarked(Boolean.valueOf(productDetailInfos[5]));
			info.setProductDeleted(Integer.valueOf(productDetailInfos[6]));

			final String key = info.getStoreID() + SP_PRODUCT_DETAIL_SEPERATOR
					+ info.getProductID();
			productMaps.put(key, info);
		}

		return productMaps;
	}
}
